package com.cy.pj.core.config;

import com.cy.pj.core.interceptor.AreaInterceptor;
import com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration;
import com.github.pagehelper.autoconfigure.PageHelperProperties;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * 拦截器类,使用spring.factories配置所以没有使用@Configuration注解
 * 目的添加自定义sql拦截器到工厂
 * @author kiven
 */
@EnableConfigurationProperties({PageHelperProperties.class})
@AutoConfigureAfter(PageHelperAutoConfiguration.class)
public class SqlInterceptorConfig {
    @Autowired
    private List<SqlSessionFactory> sqlSessionFactoryList;

    @PostConstruct
    public void loadSqlInterceptor(){
        AreaInterceptor sqlInterceptor = new AreaInterceptor();
        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
            //添加拦截器
            sqlSessionFactory.getConfiguration().addInterceptor(sqlInterceptor);
        }
    }
}
